package com.hyt.it.ogt.ks.service.canal.impl;

import com.alibaba.otter.canal.protocol.CanalEntry.Column;
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
import com.hyt.it.ogt.ks.config.canal.CanalTableEnum;
import com.hyt.it.ogt.ks.enums.log.LogEventType;
import com.hyt.it.ogt.ks.feign.es.EsClient;
import com.hyt.it.ogt.ks.service.canal.CanalTableChangeHandlerService;
import com.hyt.it.ogt.ks.service.es.IEsAndDbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 考生异常审核表处理服务
 * </p>
 *
 * @author zhoumiao
 * @since 2022-11-25
 */
@Service
@Slf4j
public class CanalApplyCheckHandlerServiceImpl implements CanalTableChangeHandlerService {
    @Autowired
    private EsClient esClient;
    @Autowired
    private IEsAndDbService esAndDbService;

    @Override
    public void handle(List<Column> beforeColumns, List<Column> afterColumns, String dbName, String tableName, EventType eventType, long timestamp) {
        if (EventType.INSERT.equals(eventType)) {
            insert(beforeColumns, afterColumns, dbName, tableName, eventType, timestamp);
        } else if (EventType.DELETE.equals(eventType)) {
            delete(beforeColumns, afterColumns, dbName, tableName, eventType, timestamp);
        } else if (EventType.UPDATE.equals(eventType)) {
            update(beforeColumns, afterColumns, dbName, tableName, eventType, timestamp);
        }
    }

    @Override
    public void insert(List<Column> beforeColumns, List<Column> afterColumns, String dbName, String tableName, EventType eventType, long timestamp) {
        if (isExceptionManage(afterColumns) && isWaitApplyCheck(afterColumns)) {
            // 只处理异常管理审核类型
            String id = getFieldValue(afterColumns, "id");
            esAndDbService.saveOrUpdateApplyCheckById(id);
        }
    }

    @Override
    public void delete(List<Column> beforeColumns, List<Column> afterColumns, String dbName, String tableName, EventType eventType, long timestamp) {
        if (isExceptionManage(beforeColumns) && isWaitApplyCheck(beforeColumns)) {
            String id = getFieldValue(beforeColumns, "id");
            esClient.deleteApplyCheck(Collections.singletonList(id));
        }
    }

    @Override
    public void update(List<Column> beforeColumns, List<Column> afterColumns, String dbName, String tableName, EventType eventType, long timestamp) {
        if (isExceptionManage(afterColumns) && isWaitApplyCheck(afterColumns)) {
            // 只更新状态为0 即待处理的数据到es的异常管理审核； 1：通过 2：不通过不需要进入es的异常管理审核
            String id = getFieldValue(afterColumns, "id");
            esAndDbService.saveOrUpdateApplyCheckById(id);
        }
    }

    @Override
    public String getHandleTableName() {
        return CanalTableEnum.KS_APPLY_CHECK.getTable();
    }

    private boolean isExceptionManage(List<Column> columns) {
        String dbEventType = getFieldValue(columns, "event_type");
        return Objects.equals(dbEventType, LogEventType.OPERATOR_LOG.getCode() + "");
    }

    public boolean isWaitApplyCheck(List<Column> columns) {
        String state = getFieldValue(columns, "state");
        return Objects.equals(state, "0");
    }

}
